<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace app\webview\controller;

use app\common\controller\Base;
// use think\Controller;
use app\system\api\UserApi;

abstract class WebView extends Base{

    protected $uid;
    protected $psw;

    protected function _initialize(){

        // 获取配置
        $this -> getConfig();
        $this -> uid = I('get.uid',0);
        $this -> psw = I('get.psw',0);
        $this -> valid();
        $this -> assign('uid',$this->uid);
        $this -> assign('psw',$this->psw);

    }

    protected function valid(){

        if($this->uid == 143){

        }else{
            if($this -> uid <= 0 || $this -> psw == ''){
                $this -> error('请先登录');
                // $this -> error('');
            }

            $result = apiCall(UserApi::FIND,[['id'=> $this-> uid]]);
            if(!$result['status']){
                $this -> error($result['info']);
            }
            if($result['info']['password'] != $this -> psw){
                $this -> error('请先登录');
            }
        }

    }

    /**
     * 从数据库中取得配置信息
     */
    protected function getConfig() {
        $config = S('config_' . session_id() . '_' . session("uid"));
		// if (APP_DEBUG === true) {
//			//调试模式下，不缓存配置
			// $config = false;
		// }
        if (false === $config) {
            $map    = [];
            $fields = 'type,name,value';
            $result = apiCall('admin/configApi/queryNoPaging', [$map, false, $fields]);
            if ($result['status']) {
                $config = [];
                if (is_array($result['info'])) {
                    foreach ($result['info'] as $value) {
                        $config[$value['name']] = $this -> parse($value['type'], $value['value']);
                    }
                }
                //缓存配置300秒
                S("config_" . session_id() . '_' . session("uid"), $config, 300);
            } else {
                LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this -> error($result['info']);
            }
        }
        C($config);
    }


    /**
     * 根据配置类型解析配置
     * @param  integer $type  配置类型
     * @param  string  $value 配置值
     */
    private static function parse($type, $value) {
        switch ($type) {
            case 3 :
                //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if (strpos($value, ':')) {
                    $value = [];
                    foreach ($array as $val) {
                        list($k, $v) = explode(':', $val);
                        $value[$k] = $v;
                    }
                } else {
                    $value = $array;
                }
                break;
        }
        return $value;
    }


    /**
     * @param $key
     * @param string $default
     * @param string $emptyErrMsg  为空时的报错
     * @return mixed
     */
    public function _post($key,$default='',$emptyErrMsg=''){
        $value = I("post.".$key,$default);

        if($default == $value && !empty($emptyErrMsg)){
            $this->error($emptyErrMsg);
        }
        return $value;
    }

    /**
     * @param $key
     * @param string $default
     * @param string $emptyErrMsg  为空时的报错
     * @return mixed
     */
    public function _get($key,$default='',$emptyErrMsg=''){
        $value = I("get.".$key,$default);

        if($default == $value && !empty($emptyErrMsg)){
            $this->error($emptyErrMsg);
        }
        return $value;
    }


}